/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_Registry.
	/// </summary>
	public class Block_Registry : Block
	{

		private enum OPERATION_TYPE
		{
			SetValue = 1,
			GetValue = 2,
			CreateKey = 3,
			CreateValue = 4,
			DeleteKey = 5,
			DeleteValue = 6
		}

		private OPERATION_TYPE _operationType;
		private string _param1 = "";
		private string _param2 = "";
		private string _param3 = "";
		private string _param4 = "";

		public override TYPE GetBlockType()
		{
			return TYPE.Registry;
		}

		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_Registry::GetXML Started.");	
			// <RegistryFunc name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<RegistryFunc name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...
			xml += GetControlPanelMessageXML();
			
			// ...command="ID"...
			// Operation types in studio is -1 from .seq, adjust.
			xml += " command=\"" + ((int)_operationType-1) + "\"";

			// ...param1="PARAMETER 1"...
			xml += " param1=\"" + Utils.XmlString(_param1) + "\"";
			// ...param2="PARAMETER 2"...
			xml += " param2=\"" + Utils.XmlString(_param2) + "\"";
			// ...param3="PARAMETER 3"...
			xml += " param3=\"" + Utils.XmlString(_param3) + "\"";
			// ...param4="PARAMETER 4"...
			xml += " param4=\"" + Utils.XmlString(_param4) + "\"";

			// End the header xml-part.
			xml += ">\r\n";

			// <Inputs>
			xml += GetInputsXML(_blockId);
		
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();

			// </RegistryFunc>
			xml +="\t\t\t</RegistryFunc>\r\n";
			return xml;
		}


		public Block_Registry(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.
			
			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, 0));
			outputErrorIds.Add(new BlockOutput(OUTPUT.NotFound_3, 94));

			Trace.Debug("Block_Registry created.");

			_currentPos += 16;

			_operationType = (OPERATION_TYPE)rawData[_currentPos];

			_currentPos += 4;

			if (_operationType == OPERATION_TYPE.CreateValue)
			{
				_param1 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
				_param2 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
				_param3 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
				_param4 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
			}
			else if (_operationType == OPERATION_TYPE.SetValue ||
				_operationType == OPERATION_TYPE.GetValue)
			{
				_param1 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
				_param2 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
				_param3 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
			}
			else if (_operationType == OPERATION_TYPE.DeleteValue)
			{
				_param1 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
				_param2 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
			}
			else if (_operationType == OPERATION_TYPE.CreateKey || 
				_operationType == OPERATION_TYPE.DeleteKey)
			{
				_param1 = Utils.DataToString(rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
			}

			Trace.Debug("Block_Registry: OpType: " + _operationType + " Param1: " + 
				_param1 + " Param2: " + _param2 + " Param3: " + _param3 + 
				" Param4: " + _param4);
		}
	}
}
